import { InjectionKey } from 'vue'
import { createStore, Store } from 'vuex'

export interface IPageItem {
  id?: string,
  path: string,
  name: string,
  file: string,
  parent: string
}
export interface IState {
  pageTreeData: IPageItem[]
}

export const key: InjectionKey<Store<IState>> = Symbol()

export default createStore<IState>({
  state: {
    pageTreeData: JSON.parse(localStorage.getItem('PAGE_TREE_DATA') || '[]')
  },
  mutations: {
    SET_PAGE_TREE_DATA(state, data){
      state.pageTreeData = data
    }
  },
  actions: {
    setPageTreeData({commit}, data) {
      localStorage.setItem('PAGE_TREE_DATA', data)
      commit('SET_PAGE_TREE_DATA', data)
    }
  },
  getters: {
    pageTreeData(state) {
      return state.pageTreeData
    }
  }
})
